package com.ruoyi.web.controller.dormitory;

import java.util.List;

import com.ruoyi.dormitory.domain.DormitoryRecord;
import com.ruoyi.dormitory.service.IDormitoryRecordService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 宿舍住宿记录Controller
 *
 * @author ruoyi
 * @date 2023-04-26
 */
@Controller
@RequestMapping("/dormitory/dormitoryRecord")
public class DormitoryRecordController extends BaseController {
    private String prefix = "dormitory/dormitoryRecord";

    @Autowired
    private IDormitoryRecordService dormitoryRecordService;

    @RequiresPermissions("dormitory:dormitoryRecord:view")
    @GetMapping()
    public String dormitoryRecord() {
        return prefix + "/dormitoryRecord";
    }

    /**
     * 查询宿舍住宿记录列表
     */
    @RequiresPermissions("dormitory:dormitoryRecord:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(DormitoryRecord dormitoryRecord) {
        startPage();
        List<DormitoryRecord> list = dormitoryRecordService.selectDormitoryRecordList(dormitoryRecord);
        return getDataTable(list);
    }

    /**
     * 导出宿舍住宿记录列表
     */
    @RequiresPermissions("dormitory:dormitoryRecord:export")
    @Log(title = "宿舍住宿记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(DormitoryRecord dormitoryRecord) {
        List<DormitoryRecord> list = dormitoryRecordService.selectDormitoryRecordList(dormitoryRecord);
        ExcelUtil<DormitoryRecord> util = new ExcelUtil<DormitoryRecord>(DormitoryRecord.class);
        return util.exportExcel(list, "宿舍住宿记录数据");
    }


    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }


//    /**
//     * 新增宿舍住宿记录
//     */
//    @GetMapping("/add/{dormitoryNumber}")
//    public String add(@PathVariable("dormitoryNumber") Integer dormitoryNumber) {
//        DormitoryRecord dormitoryRecord = new DormitoryRecord();
//        dormitoryRecord.setDormitoryNumber(dormitoryNumber);
//        return prefix + "/add";
//    }

    /**
     * 新增保存宿舍住宿记录
     */
    @RequiresPermissions("dormitory:dormitoryRecord:add")
    @Log(title = "宿舍住宿记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(DormitoryRecord dormitoryRecord) {

        return toAjax(dormitoryRecordService.insertDormitoryRecord(dormitoryRecord));
    }

    /**
     * 修改宿舍住宿记录
     */
    @RequiresPermissions("dormitory:dormitoryRecord:edit")
    @GetMapping("/edit/{recordNumber}")
    public String edit(@PathVariable("recordNumber") Integer recordNumber, ModelMap mmap) {
        DormitoryRecord dormitoryRecord = dormitoryRecordService.selectDormitoryRecordByDormitoryNumber(recordNumber);
        System.out.println(dormitoryRecord.getDormitoryNumber());
        mmap.put("dormitoryRecord", dormitoryRecord);
        return prefix + "/edit";
    }

    /**
     * 修改保存宿舍住宿记录
     */
    @RequiresPermissions("dormitory:dormitoryRecord:edit")
    @Log(title = "宿舍住宿记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(DormitoryRecord dormitoryRecord) {
        return toAjax(dormitoryRecordService.updateDormitoryRecord(dormitoryRecord));
    }

    /**
     * 删除宿舍住宿记录
     */
    @RequiresPermissions("dormitory:dormitoryRecord:remove")
    @Log(title = "宿舍住宿记录", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(dormitoryRecordService.deleteDormitoryRecordByDormitoryNumbers(ids));
    }


    @RequiresPermissions("dormitory:dormitoryRecord:detailList")
    @Log(title = "宿舍住宿人", businessType = BusinessType.OTHER)
    @PostMapping("/detailList")
    @ResponseBody
    public TableDataInfo selectDormitoryRecordById(String dormitoryId) {
        startPage();
        List<DormitoryRecord> dormitoryRecordList = dormitoryRecordService.selectDormitoryRecordListById(dormitoryId);
        return getDataTable(dormitoryRecordList);
    }
}
